<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartOrderPrinterExcel')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

include_spip('classes/order/AlohacartOrderPrinter');
include_spip('inc/filtres');
 /** 
 *  This class provides Excel order printer
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class AlohacartOrderPrinterExcel extends AlohacartOrderPrinter {    	
	
	public function __construct() {
		parent::__construct();
		
		require_once _DIR_PLUGIN_EXCEL."pear/Spreadsheet/Excel/Writer.php";
	}
	
    /**
     *  Generates Excel file for order and outputs it to stream
     *  @param int $id_order ID of order for wich Excel file will be created
     */
    public function OutputFile($id_order) {
        if (!$id_order = intval($id_order)) return false;
        
        // Creating a workbook
        $workbook = new Spreadsheet_Excel_Writer();    
        $workbook->setTempDir(_DIR_CACHE);
        $workbook->setVersion(8);
        
        // sending HTTP headers
        $workbook->send('order_'.$id_order.'.xls');
    
        // Let's create worksheet:
        $this->addWorksheet($workbook, $id_order);
        
        // Let's send the file
        $workbook->close();
    
        // After file has been sent, just exit:
        exit;
    }
	
	/**
	 *  Generates Excel file for order
	 */
	public function GenerateFile($id_order, $filename = '') {
		if (!$id_order = intval($id_order)) return false;
		// Creating a workbook
        if  ($filename)
            $workbook = new Spreadsheet_Excel_Writer(_DIR_TMP.$filename);            
        else
            $workbook = new Spreadsheet_Excel_Writer('order_'.$id_order.'.xls');                
        
        $workbook->setVersion(8);
    
        // Let's create worksheet:
        $this->addWorksheet($workbook, $id_order);
        
        // Let's send the file
        $workbook->close();
	}
    
    /**
     *  Adds Worksheet with order products to given $workbook
	 *  @param int $id_order ID of order for wich invoice will be created 
     */
    private function addWorksheet(&$workbook, $id_order) {
		// Retrieve order info and meta data of invoice extension
		$order = $this->order_mdl->select_by_id($id_order);
        $meta_data = $this->extension->GetMetaData();
        $order_products = $this->order_product_mdl->select('id_order='.$id_order);
        $currency = $this->currency_mdl->select_by_id($order['id_currency']);
		
		// Bold format
        $format_bold =& $workbook->addFormat();
        $format_bold->setBold();
        
        // Bold format with centered text
        $format_bold_centered =& $workbook->addFormat();
        $format_bold_centered->setBold();
        $format_bold_centered->setHAlign('center');        
		
        // Creating worksheet    
        $worksheet =& $workbook->addWorksheet('Order');
        $worksheet->setInputEncoding('utf-8');
        
        // Set width of columns:
        $worksheet->setColumn(0,0,3);
        $worksheet->setColumn(1,1,21);
        $worksheet->setColumn(2,3,12);
        $worksheet->setColumn(4,4,4);
        $worksheet->setColumn(5,7,15);
		
		$i = 0;
        
        // Order title
        $format_order_title = &$workbook->addFormat();
        $format_order_title->setBold();
		$format_order_title->setSize(12);
		$format_order_title->setHAlign('center');
		$worksheet->write($i, 0, _T('alohacart:order_number', array('id_order'=>$id_order)), $format_order_title);		
		$worksheet->mergeCells($i, 0, $i, 7);
		$i++;
        
        // Order's status
        $worksheet->write($i, 0, _T('alohacart:order_status').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($order['statut_titre']));
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 4);
        $i++;
        
        // Name
        $worksheet->write($i, 0, _T('alohacart:name').':', $format_bold);
        $worksheet->write($i, 2, $order['name']);
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 4);
        $i++;
        
        // Phone
        $worksheet->write($i, 0, _T('alohacart:phone').':', $format_bold);
        $worksheet->write($i, 2, $order['phone']);
        $worksheet->mergeCells($i, 0, $i, 1);
        //$worksheet->mergeCells($i, 2, $i, 4);
        $i++;
        
        // Email
        $worksheet->write($i, 0, _T('alohacart:email').':', $format_bold);
        $worksheet->write($i, 2, $order['email']);
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 4);
        $i++;
        
        // City
        $worksheet->write($i, 0, _T('alohacart:city').':', $format_bold);
        $worksheet->write($i, 2, $order['city']);
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 4);
        $i++;
        
        // Address
        $worksheet->write($i, 0, _T('alohacart:address').':', $format_bold);
        $worksheet->write($i, 2, $order['address']);
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;
        
        // Comments
        $worksheet->write($i, 0, _T('alohacart:order_comments').':', $format_bold);
        $worksheet->write($i, 2, $order['order_comments']);
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;
        
        // Shipping
        $worksheet->write($i, 0, _T('alohacart:shipping_titre').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($order['shipping_titre']));
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;
        $worksheet->write($i, 0, _T('alohacart:shipping_cost').':', $format_bold);
        $worksheet->write($i, 2, $order['shipping_cost']);
        $worksheet->mergeCells($i, 0, $i, 1);
        //$worksheet->mergeCells($i, 2, $i, 3);
        $i++;/*
        $worksheet->write($i, 0, _T('alohacart:shipping_data').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($order['shipping_data']));
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;*/
        
        // Payment
        $worksheet->write($i, 0, _T('alohacart:payment_titre').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($order['payment_titre']));
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;/*
        $worksheet->write($i, 0, _T('alohacart:payment_data').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($order['payment_data']));
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;*/
        
        // Currency
        $worksheet->write($i, 0, _T('alohacart:order_currency').':', $format_bold);
        $worksheet->write($i, 2, extraire_multi($currency['titre']).' ('._T('ecatalogue:currency_rate').$order['currency_rate'].')');
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;
        
        // Total price
        $sign_pre = $currency['sign_pre']?extraire_multi($currency['sign_pre']).' ':'';
        $sign_after = $currency['sign_after']?' '.extraire_multi($currency['sign_after']):'';
        $total_price_user = $sign_pre.($order['total_price']*$order['currency_rate']).$sign_after; 
        $worksheet->write($i, 0, _T('alohacart:order_total_price').':', $format_bold);
        $worksheet->write($i, 2, $order['total_price'].' ('.$total_price_user.')');
        $worksheet->mergeCells($i, 0, $i, 1);
        $worksheet->mergeCells($i, 2, $i, 7);
        $i++;
        
        // Products table title
		$worksheet->write($i, 0, _T('alohacart:ordered_products'), $format_bold_centered);
        $worksheet->mergeCells($i, 0, $i, 7);
        $i++;
        //$i++;
		// Products table head
		$format_table_head =& $workbook->addFormat();
        $format_table_head->setBold();
		$format_table_head->setFgColor(22);
		$format_table_head->setBorder(1);
        $format_table_head->setHAlign('center');
		$worksheet->write($i, 0, "№", $format_table_head);		
		$worksheet->write($i, 1, _T('alohacart_payment_invoice:product_title'), $format_table_head);
        $worksheet->write($i, 2, '', $format_table);
        $worksheet->write($i, 3, '', $format_table);
		$worksheet->write($i, 4, _T('alohacart_payment_invoice:product_unit'), $format_table_head);		
		$worksheet->write($i, 5, _T('alohacart_payment_invoice:product_quantity'), $format_table_head);
		$worksheet->write($i, 6, _T('alohacart_payment_invoice:product_price_without_vat'), $format_table_head);
		$worksheet->write($i, 7, _T('alohacart_payment_invoice:product_summ_without_vat'), $format_table_head);
        $worksheet->mergeCells($i, 1, $i, 3);
		
        $i++;
        
        $total_with_vat = 0;
        $total_without_vat = 0;
        $total_vat = 0;
        
        // Let's add shipping to order products, so VAT and totals will be calculated for it too:
        if ($order['shipping_titre'] && $order['shipping_cost']) {
            $shipping_cost = round($order['shipping_cost']*$order['currency_rate'], $this->currency_mdl->price_precision);        
            $order_products[] = array('titre'=>_T('alohacart_payment_invoice:shipping').': '.extraire_multi($order['shipping_titre']),
                                      'id_order'=>$order['id_order'],  
                                      'quantity'=>1,
                                      'price'=>$shipping_cost,
                                      'total_price'=>$shipping_cost);    
        }
        
        // Products table
        $format_table =& $workbook->addFormat();
		$format_table->setBorder(1);
        //$format_table->setTextWrap();
        
        $vat_value = $meta_data['is_vat_payer'] ? $meta_data['vat_value'] : 0;
        $this->vat_calculator->CalculateOrderProductsVAT($order['id_order'], $meta_data['vat_type'], $vat_value, $order_products, $totals);
        	
		foreach ($order_products as $key=>$product) {            
		    $worksheet->write($i, 0, $key+1, $format_table);
            $worksheet->write($i, 1, trim(extraire_multi($product['titre'])), $format_table);
            $worksheet->write($i, 2, '', $format_table);
            $worksheet->write($i, 3, '', $format_table);
            $worksheet->write($i, 4, $currency['code'], $format_table);
            $worksheet->write($i, 5, $product['quantity'], $format_table);
            $worksheet->write($i, 6, $product['price_without_vat'], $format_table);
            $worksheet->write($i, 7, $product['total_price_without_vat'], $format_table);
            $worksheet->mergeCells($i, 1, $i, 3);
            $i++;
		}
                
        // Totals
        $format_totals =& $workbook->addFormat();
        $format_totals->setBold();
        $format_totals->setBorder(1);
        $worksheet->write($i, 6, _T('alohacart_payment_invoice:total_without_vat'), $format_totals);
        $worksheet->write($i, 7, $totals['total_without_vat'], $format_table);
        $i++;        
        $worksheet->write($i, 6, _T('alohacart_payment_invoice:vat'), $format_totals);
        $total_vat = $totals['total_vat'] ? $totals['total_vat'] : _T('alohacart_payment_invoice:without_vat'); 
        $worksheet->write($i, 7, $total_vat, $format_table);
        $i++;        
        $worksheet->write($i, 6, _T('alohacart_payment_invoice:total_with_vat'), $format_totals);
        $worksheet->write($i, 7, $totals['total_with_vat'], $format_table);
        
		$i+=3;
        $worksheet->write($i, 1, _T('alohacart_payment_invoice:wrote_out').'  _______________');        
    }
}
?>